﻿using System;
using System.Collections.Generic;
using Steamworks;

namespace Terraria.Social.Steam
{
	// Token: 0x02000141 RID: 321
	public class SteamP2PWriter
	{
		// Token: 0x06000D9B RID: 3483 RVA: 0x0000D774 File Offset: 0x0000B974
		public SteamP2PWriter(int channel)
		{
			this._channel = channel;
		}

		// Token: 0x06000D9C RID: 3484 RVA: 0x003F86C4 File Offset: 0x003F68C4
		public void QueueSend(CSteamID user, byte[] data, int length)
		{
			object @lock = this._lock;
			lock (@lock)
			{
				Queue<SteamP2PWriter.WriteInformation> queue;
				if (this._pendingSendData.ContainsKey(user))
				{
					queue = this._pendingSendData[user];
				}
				else
				{
					queue = (this._pendingSendData[user] = new Queue<SteamP2PWriter.WriteInformation>());
				}
				int i = length;
				int num = 0;
				while (i > 0)
				{
					SteamP2PWriter.WriteInformation writeInformation;
					if (queue.Count == 0 || 1024 - queue.Peek().Size == 0)
					{
						if (this._bufferPool.Count > 0)
						{
							writeInformation = new SteamP2PWriter.WriteInformation(this._bufferPool.Dequeue());
						}
						else
						{
							writeInformation = new SteamP2PWriter.WriteInformation();
						}
						queue.Enqueue(writeInformation);
					}
					else
					{
						writeInformation = queue.Peek();
					}
					int num2 = Math.Min(i, 1024 - writeInformation.Size);
					Array.Copy(data, num, writeInformation.Data, writeInformation.Size, num2);
					writeInformation.Size += num2;
					i -= num2;
					num += num2;
				}
			}
		}

		// Token: 0x06000D9D RID: 3485 RVA: 0x003F87E4 File Offset: 0x003F69E4
		public void ClearUser(CSteamID user)
		{
			object @lock = this._lock;
			lock (@lock)
			{
				if (this._pendingSendData.ContainsKey(user))
				{
					Queue<SteamP2PWriter.WriteInformation> queue = this._pendingSendData[user];
					while (queue.Count > 0)
					{
						this._bufferPool.Enqueue(queue.Dequeue().Data);
					}
				}
				if (this._pendingSendDataSwap.ContainsKey(user))
				{
					Queue<SteamP2PWriter.WriteInformation> queue2 = this._pendingSendDataSwap[user];
					while (queue2.Count > 0)
					{
						this._bufferPool.Enqueue(queue2.Dequeue().Data);
					}
				}
			}
		}

		// Token: 0x06000D9E RID: 3486 RVA: 0x003F8898 File Offset: 0x003F6A98
		public void SendAll()
		{
			object @lock = this._lock;
			lock (@lock)
			{
				Utils.Swap<Dictionary<CSteamID, Queue<SteamP2PWriter.WriteInformation>>>(ref this._pendingSendData, ref this._pendingSendDataSwap);
			}
			foreach (KeyValuePair<CSteamID, Queue<SteamP2PWriter.WriteInformation>> keyValuePair in this._pendingSendDataSwap)
			{
				Queue<SteamP2PWriter.WriteInformation> value = keyValuePair.Value;
				while (value.Count > 0)
				{
					SteamP2PWriter.WriteInformation writeInformation = value.Dequeue();
					bool flag2 = SteamNetworking.SendP2PPacket(keyValuePair.Key, writeInformation.Data, (uint)writeInformation.Size, 2, this._channel);
					this._bufferPool.Enqueue(writeInformation.Data);
				}
			}
		}

		// Token: 0x040011F5 RID: 4597
		private const int BUFFER_SIZE = 1024;

		// Token: 0x040011F6 RID: 4598
		private Dictionary<CSteamID, Queue<SteamP2PWriter.WriteInformation>> _pendingSendData = new Dictionary<CSteamID, Queue<SteamP2PWriter.WriteInformation>>();

		// Token: 0x040011F7 RID: 4599
		private Dictionary<CSteamID, Queue<SteamP2PWriter.WriteInformation>> _pendingSendDataSwap = new Dictionary<CSteamID, Queue<SteamP2PWriter.WriteInformation>>();

		// Token: 0x040011F8 RID: 4600
		private Queue<byte[]> _bufferPool = new Queue<byte[]>();

		// Token: 0x040011F9 RID: 4601
		private int _channel;

		// Token: 0x040011FA RID: 4602
		private object _lock = new object();

		// Token: 0x02000142 RID: 322
		public class WriteInformation
		{
			// Token: 0x06000D9F RID: 3487 RVA: 0x0000D7AF File Offset: 0x0000B9AF
			public WriteInformation()
			{
				this.Data = new byte[1024];
				this.Size = 0;
			}

			// Token: 0x06000DA0 RID: 3488 RVA: 0x0000D7CE File Offset: 0x0000B9CE
			public WriteInformation(byte[] data)
			{
				this.Data = data;
				this.Size = 0;
			}

			// Token: 0x040011FB RID: 4603
			public byte[] Data;

			// Token: 0x040011FC RID: 4604
			public int Size;
		}
	}
}
